package com.kjsdfgk.hjmdsf.ftmine.mvp.presenter;

import android.app.Application;
import android.text.TextUtils;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.SPUtils;
import com.jess.arms.base.App;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.PermissionUtil;
import com.jess.arms.utils.RxLifecycleUtils;
import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.entity.AccountJson;
import com.kjsdfgk.hjmdsf.app.msg.AddViewMsg;
import com.kjsdfgk.hjmdsf.ftmine.mvp.contract.MineV3FtContract;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;
import java.util.UUID;

import static com.kjsdfgk.hjmdsf.app.Config.DATA_PATH;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/30/2020 17:57
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@FragmentScope
public class MineV3FtPresenter extends BasePresenter<MineV3FtContract.Model, MineV3FtContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MineV3FtPresenter(MineV3FtContract.Model model, MineV3FtContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AddViewMsg msg) {
        for (int i = 0; i < msg.data.length; ) {

            if ("getUserInfo".equals((String) msg.data[i])) {
                String account = SPUtils.getInstance().getString("account","");
                String password = SPUtils.getInstance().getString("password","");
                if (!TextUtils.isEmpty(account)&&!TextUtils.isEmpty(password)){
                    userLogon(account ,password);
                }


            }

            i++;
            i++;
        }
    }
    public void userLogon(String account, String password) {

        mModel.userLogon(account , password )
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<AccountJson>(mErrorHandler) {
                    @Override
                    public void onError(@NonNull Throwable e) {

                        mRootView.hideLoading();


                    }

                    @Override
                    public void onNext(@NonNull AccountJson accountJson) {
                        mRootView.hideLoading();
                        ((App) mApplication).getAppComponent().extras().put("Account", accountJson.getMsg().getInfo());
                        accountJson.getMsg().getInfo().setToken(accountJson.getMsg().getToken());
                        mRootView.showAccount(accountJson.getMsg().getInfo());

                        //    (Account) ((App)mApplication).getAppComponent().extras().get("Account");
                    }
                });

    }
    public Account getAccount() {
        return (Account) ((App)mApplication).getAppComponent().extras().get("Account");
    }
    public void logOut() {
        ((App) mApplication).getAppComponent().extras().put("Account", null);
        String account = SPUtils.getInstance().getString("account","");
        String password = SPUtils.getInstance().getString("password","");
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)){
            mRootView.showMessage("别点了 ，用户未登录");
            mRootView.showInitView();
        }else {
            SPUtils.getInstance().remove("account" );
            SPUtils.getInstance().remove("password" );
            mRootView.showMessage("退出成功");
            mRootView.showInitView();
            EventBus.getDefault().post(new AddViewMsg("getUserInfo",""));
        }
    }

    public void checkLogin() {
        String account = SPUtils.getInstance().getString("account","");
        String password = SPUtils.getInstance().getString("password","");
        if (!TextUtils.isEmpty(account)&&!TextUtils.isEmpty(password)){
            userLogon(account ,password);
        }
    }
}
